# -*- coding: utf-8 -*-
"""
@Time ： 2021/12/30 13:21
@Auth ： Jolg
@File ：models.py
@IDE ：PyCharm

"""
from sqlalchemy import Column, Integer, String,DateTime, ForeignKey
from sqlalchemy.orm import declarative_base
from apps.base.db import engine,session
import time,datetime
from sqlalchemy.sql import func
from apps.base.log import logger
Base = declarative_base()

class Basejson():
    def json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]
        return dict

class Users(Base,Basejson):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True)
    name = Column(String(64), unique=True)
    passwd = Column(String(64))
    nicename = Column(String(64))
    create= Column(DateTime(), default=datetime.datetime.now())
    lastlogin = Column(DateTime())
    def __init__(self, name,nicename,passwd):
        self.name = name
        self.nicename=nicename
        self.passwd=passwd
    # def __str__(self):
    #     return self.name

class AccessRecords(Base):
    __tablename__ = "accessrecords"
    id = Column(Integer, primary_key=True)
    userid = Column(Integer)
    username = Column(String(64))
    nicename = Column(String(164))
    url = Column(String(640))
    method = Column(String(64))
    access_date =Column(DateTime(), default=datetime.datetime.now())
    # access_date =Column(DateTime,default=time.time())



def initdb():
    Base.metadata.create_all(engine)
    user=session.query(Users).filter_by(name='admin').first()
    if not user:
        adminuser=Users(name='admin', nicename="管理员",passwd='21232f297a57a5a743894a0e4a801fc3')
        session.add(adminuser)
        # session.commit();
        session.flush()
